/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.util {
	import flash.display.DisplayObject;
	import flash.system.ApplicationDomain;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	/**
	 * @static
	 */
	public class ClassHelper {
		public static function getClassName(obj:Object):String {
			var fullName:String = getQualifiedClassName(obj);
			var parts:Array = fullName.split('::');
			if (parts.length == 0) {
				return null;
			} else if (parts.length > 1) {
				return parts[1];
			} else {
				return parts[0];
			}
		}
		
		public static function getClassByName(name:String):Class {
			return Class(getDefinitionByName(name));
		}

		public static function getClass(obj:Object):Class {
			var className:String = getQualifiedClassName(obj);
			if (ApplicationDomain.currentDomain.hasDefinition(className)) {
   				return ApplicationDomain.currentDomain.getDefinition(className) as Class;
			} else {
				var displayObj:DisplayObject = obj as DisplayObject;
				if (displayObj != null && displayObj.loaderInfo.applicationDomain.hasDefinition(className)) {
					return displayObj.loaderInfo.applicationDomain.getDefinition(className) as Class;
				}
				return null;
			}
		}
		
		public static function getSuperClass(o: Object): Object {
			var n: String = getQualifiedSuperclassName(o);
			if(n == null)return(null);
			return(getDefinitionByName(n));
		}
	}
}